package net.neptech.listener.impl;

import lombok.extern.slf4j.Slf4j;
import net.neptech.model.dto.InstanceNodeRecordParamDto;
import net.neptech.listener.AbstractEventListenerStrategy;
import net.neptech.service.RemoteService;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Saya
 * @Date 2025/8/22 14:36
 * @Description 节点取消
 **/

@Slf4j
@Component
public class ActivityCanceledEventListener extends AbstractEventListenerStrategy implements InitializingBean {
    @Autowired
    private RemoteService remoteService;

    /**
     * 处理数据
     */
    @Override
    public void handle(FlowableEvent event) {
        //任务取消了
        org.flowable.engine.delegate.event.impl.FlowableActivityCancelledEventImpl activityCancelledEvent = (org.flowable.engine.delegate.event.impl.FlowableActivityCancelledEventImpl) event;
        String activityId = activityCancelledEvent.getActivityId();
        String activityName = activityCancelledEvent.getActivityName();

        String executionId = activityCancelledEvent.getExecutionId();
        String processInstanceId = activityCancelledEvent.getProcessInstanceId();

        log.info("取消的节点：{}   {} {}  {}", activityId, activityName, processInstanceId, executionId);
        DelegateExecution execution = activityCancelledEvent.getExecution();
        {
            //节点取消了
            InstanceNodeRecordParamDto instanceNodeRecordParamDto = new InstanceNodeRecordParamDto();
            instanceNodeRecordParamDto.setProcessInstanceId(processInstanceId);
            instanceNodeRecordParamDto.setExecutionId(executionId);
            instanceNodeRecordParamDto.setNodeId(activityId);
            instanceNodeRecordParamDto.setNodeName(activityName);
            remoteService.cancelNodeEvent(instanceNodeRecordParamDto);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(FlowableEngineEventType.ACTIVITY_CANCELLED.toString());
    }
}
